package com.uchengwang.ui.utils;


import android.text.TextUtils;

import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.cycle.bean.CommentConfig;
import com.uchengwang.ui.cm_ui.cycle.bean.CommentItem;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleCmtBean;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleReplyBean;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleSubPeopleSharesBean;
import com.uchengwang.ui.self.circledemo.bean.FavortItem;
import com.uchengwang.ui.self.circledemo.bean.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;



/**
 * 
* @ClassName: DatasUtil 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author yiw
* @date 2015-12-28 下午4:16:21 
*
 */
public class DatasUtil {
	public static List<User> users = new ArrayList<User>();
	/**
	 * 动态id自增长
	 */
	private static int circleId = 0;
	/**
	 * 点赞id自增长
	 */
	private static int favortId = 0;
	/**
	 * 评论id自增长
	 */
	private static int commentId = 0;

	public static User curUser;

	public static ArrayList<String> filePhotos;



	public static User getUser(int pos) {
		return users.get(pos);
	}


	public static int getRandomNum(int max) {
		Random random = new Random();
		int result = random.nextInt(max);
		return result;
	}


	/**
	 * 赞
	 * @return
	 */
	public static FavortItem createCurUserFavortItem(int posin, String FavorId) {
		FavortItem item = new FavortItem();
		item.setId(FavorId);
		User user = new User(UCommonUtil.getUtil().getId(), UChengApplication.nickName, UChengApplication.userAvatar);
		item.setUser(user);
		return item;
	}

	/**
	 * 根据返回的信息  重新编辑回复评论的格式
	 * */
	public static List<CycleSubPeopleSharesBean> createCommentItemList(List<CycleSubPeopleSharesBean> sharesBeans) {

		if (sharesBeans != null) {
			for (CycleSubPeopleSharesBean sharesBean : sharesBeans){
				//遍历信息
				List<CycleCmtBean> cycleCmtBeans = sharesBean.getComment();

				if (cycleCmtBeans != null && cycleCmtBeans.size() >0){
					List<CommentItem> items = new ArrayList<CommentItem>();

					for (CycleCmtBean cmtBean : cycleCmtBeans) {
						//循环遍历评论
						CommentItem commentItem = new CommentItem();

						String content = cmtBean.getComment();
						long cmtId = cmtBean.getId();//所属评论ID
						long createTime = cmtBean.getCreateTime();//回复时间
						String nickName = cmtBean.getNickName();//回复人昵称
						String userAvatar = cmtBean.getUserAvatar();
						long userId = cmtBean.getUid();//回复人ID
						if (!TextUtils.isEmpty(cmtBean.getRemarkNickName())){
							nickName = cmtBean.getRemarkNickName();
						}

						User user = new User(userId, nickName, userAvatar);

						commentItem.setUser(user);
						commentItem.setContent(content);
						commentItem.setTime(createTime);
						commentItem.setId(cmtId);
						items.add(commentItem);

						List<CycleReplyBean> replyBeanList = cmtBean.getReply();
						if (replyBeanList != null && replyBeanList.size() >0){
							//循环遍历评论里的回复
							for (CycleReplyBean replyBean : replyBeanList){
								CommentItem commentItemR = new CommentItem();

								long replyUserId = replyBean.getReplyUid();//被回复人ID
								String replyNickName = replyBean.getReplyNickName();//被回复人昵称
								String replyUserAvatar = replyBean.getReplyUserAvatar();
								String replyContent = replyBean.getReplyContent();

								long id = replyBean.getId();
								long replyId = replyBean.getReplyId();



								String nickNameR = replyBean.getNickName();//回复人昵称
								String userAvatarR = replyBean.getUserAvatar();
								long userIdR = replyBean.getUid();//回复人ID


								User userR = new User(userIdR, nickNameR, userAvatarR);
								User touser = null;
								if(!TextUtils.isEmpty(replyNickName)){
									//被回复的人 不为空
									touser = new User(replyUserId, replyNickName, replyUserAvatar);
								}else {
									//否则取评论人信息
									touser = new User(userId, nickName, userAvatar);
								}

								if (replyContent != null ) {
									commentItemR.setUser(userR);
									commentItemR.setToReplyUser(touser);
									commentItemR.setContent(replyContent);
									commentItemR.setTime(createTime);
									commentItemR.setCmtId(cmtId);
									commentItemR.setId(id);
									commentItemR.setReplyId(replyId);

									items.add(commentItemR);
								}

							}
						}
					}

					sharesBean.setCommentItemLists(items);
				}
			}
		}

		return sharesBeans;
	}

	/**
	 * 创建发布评论
	 *
	 * @return
	 */
	public static CommentItem createPublicComment(final String content, CommentConfig config) {
		final CommentItem item = new CommentItem();
//        String id = circleDataList.get(config.circlePosition).getId();
//        item.setContent(content);
//        DynamicCommentManager.getInstance().commentDynamic(UUApplication.uuid, UUApplication.NickName, Long.valueOf(id), content, new IDynamicCommentManager.OnCommentDynamicListener() {
//            @Override
//            public void onCommentDynamic(DynamicRemark comment) {
////                if (BuildConfig.DEBUG)
////                    Log.d("DatasUtil", comment.getContent() + comment.getRemarkName());
//                long dynamicId = comment.getDynamicId();
//                String remarkName = comment.getUserNickName();
//                long remarkId = comment.getRemarkId();
//                String content1 = comment.getContent();
//                item.setId(String.valueOf(dynamicId));
//                item.setUser(curUser);
//            }
//        });
		return item;
	}

	/**
	 * 创建回复评论
	 *
	 * @return
	 */
	public static CommentItem createReplyComment(CommentConfig config, String content) {
		CommentItem item = new CommentItem();
//        String id = circleDataList.get(config.circlePosition).getComments().get(0).getRemarkId(); //评论id
//        if (id != null) {
//            DynamicCommentManager.getInstance().cmtrepDynamic(Long.valueOf(id), UUApplication.uuid, UUApplication.NickName, content, Long.valueOf(config.replyUser.getId()), new IDynamicCommentManager.OncmtrepDynamicListener() {
//                @Override
//                public void onCmtrepDynamic(DynamicCmtrepResp cmtrepResp) {
//                    cmtrepResp.getReplyId();
//                }
//            });
//            item.setId(id);
//            item.setContent(content);
//            item.setUser(curUser);
//            item.setToReplyUser(config.replyUser);
//        }
		return item;
	}
}
